<?php

/**
 * Test class for uploading and rendering files and images
 *
 * @author davidmorris
 */
class Filetest extends CI_Controller {
    
    public function index()
    {   

        $Location = $this->config->item('assets_path').'images/';        
        $this->load->library('ImageUploader', array('Table' => 'files_temp', 'Dir' => $Location , 'Field' => 'newfile'),'FU');
        if(count($_FILES) && $_FILES['upload']['error'] == UPLOAD_ERR_OK)
        {
            $num = $this->FU->insert('upload',array("datetime" => date('y-m-d h:i:s'), 'unix'=>time()),'My Test Caption');
            echo "success".$num;
        }
        
        $this->load->view('filetest');
    }
    
    public function modeltest()
    {
        $this->load->model('TaskModel');
        $this->TaskModel->update(2);
    }
    
    public function form()
    {

        $data = array(
            'parentTask' => array('-1' => 'None'),
            'project'    => array('-1' => 'None')
            
        );
        $this->load->helper('url');
        $this->load->view('tasks_add_form', $data);
    }
    
    public function upload()
    {
        
        $path = $this->config->item('assets_path').'images/'.$_GET['qqfile']; 
        $serial = serialize($_FILES);
        $this->load->database();
        $this->db->query('INSERT INTO dave_test (message) VALUES (?)', array(serialize($_GET)));
        try {
            $input = fopen("php://input", "r");
            $temp = tmpfile();
            $realSize = stream_copy_to_stream($input, $temp);
            fclose($input);
            $target = fopen($path, "w");        
            fseek($temp, 0, SEEK_SET);
            stream_copy_to_stream($temp, $target);
            fclose($target);
        } catch(Exception $E)
        {
            die(json_encode(array("error" => $E->getMessage())));
        }
        die(json_encode(array('success' => true)));
    }
}